var totalCount=0;
var commitCount = 0;
var radom01=parseInt(Math.random()*2);
var colorsSelect=["#00b150","#ed7c30"];
var colorSelect=colorsSelect[radom01];

var colors=["#f63a0f","#f27011","#f2b01e","#f2d31b","#86e01e"];
colors[0]=colorsSelect[radom01];

$(function(){
    pushHistory();

    window.addEventListener("popstate", function(e) {
        var length = window.history.length;
        var position = -1;

        //-扫描二维码进来的-
        if(typeof(taskCategory) != "undefined" && taskCategory.indexOf('N') > -1)
        {
            window_close();
            pageClose();
        }
        else
        {
            if(length > 4)
            {
                position = 4 - length;
            }
            else if(length > 2)
            {
                position = 2 - length;
            }
        }

        window.history.go(position);

    }, false);

    function pushHistory(){
        var state = {
            page:"questionTemplete"
        };

        window.history.pushState(state,"title","#");
    }
});

var totalCount=0;
var startTime;

function dealList(){
    if(result.code==200){
        $("#title").text(taskName);
        $.each(result.data,function(index,item){
            totalCount++;
            var itemContent =$(".subjectContent").eq(0).clone();
            itemContent.css("display","block");
            itemContent.data("datas",item);
            itemContent.insertAfter($(".subjectContent").last());

            $(".titleContent",itemContent)[0].innerText=(index+1)+" . "+item.rlibContent;

            if(item.rlibRemark==""||item.rlibRemark==undefined){
                $(".question_about_Image",itemContent).css("display","none");
                $(".titleContent",itemContent).css("margin-right","15");

            }else{
                $(".titleContent",itemContent).css("margin-right","40");
                $(".question_about_Image",itemContent).css("display","inline-block");
                $(".question_about_text",itemContent)[0].innerText=item.rlibRemark;
            }

            $.each(item.optArray,function(index1,item1){

                var itemSelection =$(".selections").eq(0).clone();
                itemSelection.css("display","block");
                itemSelection.data("datas",item1);
                $(".selectButton",itemSelection).attr("name","selectButton"+index);

                // if(index1==0){
                //     $(".realText",itemSelection)[0].innerText=item1.optContent+"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
                // }else{
                //     $(".realText",itemSelection)[0].innerText=item1.optContent;
                // }

                $(".realText",itemSelection)[0].innerText=item1.optContent;

                itemContent.append(itemSelection);
            });
        });

        $(".question_about_Image").click(function(){
            var line =$(this).parent().parent();
            var param= line.find(".question_about_content");

            var style = param.css("display");
            if(style == "none")
            {
                param.css("display","block");
            }
            else {
                param.css("display", "none");
            }
        });

    }else{
        alert("获取题目列表失败");
    }
}

dealList();
$("#progressText").text("0 / "+totalCount);

startTime=new Date().getTime();

$(".selectButton").each(function (index) {
    $(this).change(function () {

        var selectTextClass=radom01==0?"selectTextSlect":"selectTextSlect1";

        if($(this).is(':checked')){
            var scopeParent=$(this).parent();
            $(".realText",scopeParent).attr("style","color:"+colorSelect);
            $(this).parent().addClass(selectTextClass);
            var scope=$(this).parent().parent().siblings();
            $(".selectButton",scope).attr("checked",false);
            $(".selectText",scope).removeClass(selectTextClass);
            $(".realText",scope).attr("style","color:#777777");

            var totalCurrent=$("."+selectTextClass).length;

//            $(".subjectContent").each(function (no) {
//
//                if(no>0){
//                    var index=(no-1);
//                    var selectName="selectButton"+index;
//                    var itemSelect=$("input[name='"+selectName+"']:checked",$(this));
//                    if(itemSelect.length!=0){
//                        totalCurrent++;
//                    }
//                }
//            });

            $("#progressText").text(totalCurrent+" / "+totalCount);

            var rate=parseInt((totalCurrent/totalCount)*100);
            var index=parseInt(rate/20);

            if(index<0){
                index=0;
            }
            if(index>4){
                index=4;
            }
            color=colors[index];

            $(".progress-bar").css("width",rate+"%");
            $(".progress-bar").css("background-color",color);

        }else{
            //else不会执行
            //scopeParent.removeClass("selectTextSlect");
        }
    });
});

$("#progress_btnCommit").click(function () {
	commitAnswer();
});

function commitAnswer()
{
	commitCount ++;
	
    var params={};
    var paramsAnswers={};

    var endTime=new Date().getTime();

    paramsAnswers.toolId=toolId;
    paramsAnswers.taskId=taskId;
    paramsAnswers.uid=uid;
    paramsAnswers.tel=phone;
    paramsAnswers.answerTime=parseInt((endTime-startTime)/60000);
    var rlibsList=new Array();

    var isPass=true;
    $(".subjectContent").each(function (no) {

        if(no>0){
            var index=(no-1);
            var selectName="selectButton"+index;
            var itemSelect=$("input[name='"+selectName+"']:checked",$(this));
            if(itemSelect.length==0){
                alert("第"+(index+1)+"题没有选,请选择");
                isPass=false;
                return false;
            }
            var data=$(this).data("datas");
            var data1=itemSelect.parent().parent().data("datas");

            var itemSelectParams={};
            itemSelectParams.rlibId=data.id;
            itemSelectParams.optId=data1.optNo;
            rlibsList[index]=itemSelectParams;
        }
    });

    if(isPass){
        paramsAnswers.rlibsList=rlibsList;
        params.answers=paramsAnswers;
        params.sysParam = mSysParam;
        
        if(commitCount < 2)
        {
        	OCwindows("dialogFloat");	
        }
        
        var url="/gaigo-open-shre/api/task/openCommitSubject.htm";
        var listener=dealCommit;
        
        Request("POST",JSON.stringify(params),url,listener);
    }	
}

function dealCommit(resultObject){
    if(resultObject.code=="200" || resultObject.code == "433"){
        OCwindows("dialogFloat");
        $("progress_btnCommit").attr('disabled',true);

        $("#contentAnswer").css("display","none");
        $("#contentSuc").css("display","block");

        if(taskCategory== "NQ" || (taskType == 1 && taskCategory == "Y" ) ){
            $("#taskToReport").css("display","block");
            timedCount();
        }
        else
        {
            $("#taskToReport").css("display","none");
        }

    }else{
    	if(commitCount < 2)
    	{
			setTimeout("commitAnswer()",500);	
    	}
    	else
    	{
            OCwindows("dialogFloat");
   	       alert("提交失败！ "+resultObject.message+":"+resultObject.code);		
    	}
    }
}

$("#btnClose").click(function () {

    var a = window.confirm("是否确定退出,退出后您此次答题将失效?");
    if(a==true){
        window.close();
    }
});
